package com.oocl.processdData.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.oocl.processdData.pojo.Customer;

public class CheckoutUtil {

	public static Set<Customer> checkAll(Collection<Customer> custs){
		Set<Customer> customers = new HashSet<Customer>();
		for(Customer c:custs){
			if(checkName(c) && checkEmail(c) && checkSex(c) && checkBirthday(c)){
				customers.add(c);
			}
		}
		return customers;
	}
	
	public static boolean checkName(Customer cust) {
		String regex =  "^(([\u4e00-\u9fa5]{1,20})|([a-zA-Z]{1,16}))$";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(cust.getName());	
		return matcher.find();
	}

	public static boolean checkEmail(Customer cust) {
		String regex="[a-zA-Z0-9_\\-\\.]+@[a-zA-Z0-9]+(\\.(com|cn|org|edu|hk))";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(cust.getEmail());
		return matcher.find();
	}

	public static boolean checkSex(Customer cust) {
		String regex = "true$|false$";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(cust.getSex());
		return matcher.find();
	}

	public static boolean checkBirthday(Customer cust) {
		String regex = "^((\\d{2}(([02468][048])|([13579][26]))[-\\s]?((((0?[13578])|(1[02]))[-\\s]?((0?[1-9])"
				+ "|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[-\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[-\\s]?((0?[1-9])"
				+ "|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[-\\s]?((((0?[13578])|(1[02]))[-\\s]"
				+ "?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[-\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[-\\s"
				+ "]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(cust.getBirthday());
		return matcher.find();
	}

	

}
